<template>  
    <div class="container">   
      <div class="main">  
        <div class="header">  
          <el-button type="warning" @click="ckRow(row)">批量出库</el-button>  
        </div>  
        <el-table :data="tableData" border class="eltable" @selection-change="handleSelectionChange">  
          <el-table-column type="selection" width="55"></el-table-column>
          <el-table-column prop="llck_zt" label="状态" width="80">
            <template #default="{ row }">
            <span v-if="row.llck_zt === 1">未出库</span>
            <span v-else-if="row.llck_zt === 2">已出库</span>
          </template>
          </el-table-column>  
          <el-table-column prop="wl_code" label="物料编码"></el-table-column>  
          <el-table-column prop="llckxx_wlmc" label="物料名称"></el-table-column>  
          <el-table-column prop="dictionary_name" label="物料分类"></el-table-column>  
          <el-table-column prop="dw_name" label="物料单位" ></el-table-column>  
          <el-table-column prop="llckxx_cksl" label="出库数量" ></el-table-column>  
          <el-table-column prop="ck_name" label="出库仓库"></el-table-column>  
          <el-table-column label="操作" width="300">  
            <template #default="{ row }">  
              <el-button type="text" @click="ckRow(row)">出库</el-button>  
            </template>  
          </el-table-column>  
        </el-table>  
        <div class="button-container">
                <el-button type="danger" @click="onCancel">返回</el-button>
            </div>
      </div>  
    </div>  
    
  </template>  
  
  <script>  
  import axios from 'axios';
  import { ref,onMounted } from 'vue';  
  import { useRoute,useRouter } from 'vue-router';
  import { ElMessage, ElMessageBox } from 'element-plus';
  
  export default {  
    setup() {  
        const router = useRouter();
        const route = useRoute();
      const searchKey = ref(''); 
      const row = ref(null); 
      const treeData = ref([]);  
      const defaultProps = {  
        children: 'children',  
        label: 'label',  
      };  
  
      const tableData = ref([]);  
      const  selectedRows= ref([]);  
      const selectedFilter = ref('');  
      const searchTable = ref('');  
      const currentPage = ref(1);  
      const pageSize = ref(2);  
      const totalItems = ref(3);  

const sx = (row) => {  
        // 添加逻辑  
        axios.get('/api/WMS_LLCKXX/Getckxx?code='+row)
            .then(function (response) {
                console.log(response.data);
                tableData.value = response.data
            })
            .catch(function (error) {
                console.error('错误:', error);
            })
        };
        const handleSelectionChange = (selection) => {
      selectedRows.value = selection;
      console.log('选中的行数据:', selectedRows.value);
    };
        const onCancel = () => {
            router.push({ path: '/PickingWarehouse' });
        } 
      const add = () => { 
        
        // 添加逻辑  
      };  
      const importData = () => {  
        // 导入逻辑  
      };  
  
      const query = () => {  
        // 查询逻辑  
        console.log('Querying with:', searchTable.value);  
      };  
     
  
      // const ckRow = async(row) => {  
      //   // 编辑行逻辑  
      //   console.log('Editing row:', row);  
      //   const t=await axios.get('/api/WMS_KC/GetWMS_KCCKDL?ck_code='+row.ck_code+'&wl_code='+row.wl_code)
      //   console.log(t.data);

      //   const response = await axios.put('/api/WMS_LLCK/UpdateWMS_LLCK?code='+row.llck_code);
      //   console.log(response.data);
      //   const ck = await axios.put('/api/WMS_KC/UpdateWMS_KCSL?code='+t.data+'&sl='+row.llckxx_cksl);
      //   console.log(ck.data);
      //    ElMessage.error('出库成功');
      //   router.push({ path: '/PickingWarehouse' });
      // };  
      const ckRow = async (row) => {  
  // 如果 row 存在，说明是单条出库
  if (row) {
    try {
      console.log('Editing row:', row);  
        const t=await axios.get('/api/WMS_KC/GetWMS_KCCKDL?ck_code='+row.ck_code+'&wl_code='+row.wl_code)
        console.log(t.data);

        const response = await axios.put('/api/WMS_LLCK/UpdateWMS_LLCK?code='+row.llck_code);
        console.log(response.data);
        const ck = await axios.put('/api/WMS_KC/UpdateWMS_KCSL?code='+t.data.kc_code+'&sl='+row.llckxx_cksl);
        console.log(ck.data);
         ElMessage.success('出库成功');
        router.push({ path: '/PickingWarehouse' });
    } catch (error) {
      console.error('出库失败:', error);
       ElMessage.error('出库失败，请检查日志');
    }
  } else {
    // 如果 row 不存在，说明是批量出库
    if (selectedRows.value.length === 0) {
       ElMessage.error('请至少选择一行进行出库操作');
      return;
    }

    try {
      for (const row of selectedRows.value) {
        const t=await axios.get('/api/WMS_KC/GetWMS_KCCKDL?ck_code='+row.ck_code+'&wl_code='+row.wl_code)
        console.log(t.data);

        const response = await axios.put('/api/WMS_LLCK/UpdateWMS_LLCK?code='+row.llck_code);
        console.log(response.data);
        const ck = await axios.put('/api/WMS_KC/UpdateWMS_KCSL?code='+t.data.kc_code+'&sl='+row.llckxx_cksl);
        console.log(ck.data);
     

        console.log(`出库成功: ${row.llck_code}`);
      }

       ElMessage.success('批量出库成功');
      router.push({ path: '/PickingWarehouse' });
    } catch (error) {
      console.error('批量出库失败:', error);
       ElMessage.error('批量出库失败，请检查日志');
    }
  }
};  
   
  
      const handleCurrentChange = (page) => {  
        currentPage.value = page;  
        // 分页逻辑  
        console.log('Current page:', page);  
      };  
      onMounted(() => {
        const rowData = route.query.row;
            if (rowData) {
                row.value = JSON.parse(rowData); // 反序列化 JSON 字符串
                console.log('Received row data:', row.value);
                console.log('1 1 1:', row.value.llck_code);
            }
            sx(row.value.llck_code);
        });
  
      // 确保所有函数都被返回  
      return {  
        searchKey,  
        treeData,  
        defaultProps,  
        tableData,  
        selectedFilter,  
        searchTable,  
        currentPage,  
        pageSize,  
        totalItems,  
        add,   
        importData,  
        query,  
        ckRow,    
        handleCurrentChange,  
        onCancel,
        handleSelectionChange,
      };  
    },  
  };  
  </script>  
  
  <style>  
.container {  
  display: flex;  
  gap: 20px;
  height: 95%;
}  

.sidebar {  
  height: 100%;
  
}  

.main {  
  width: 80%;
  flex: 1;  
  height: 100%;
}  

.header {  
   display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 20px;
}  
.eltable{
  height: 95%;
}
.letree{
  height: 100%;
}
</style>